package com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.goods

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.global.AppGlobals
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.chlid.SecKillSubFragment
import com.example.shushiyunsst.util.GOODS_COMMENT
import com.example.shushiyunsst.util.GOODS_ID
import com.google.android.material.tabs.TabLayout
import com.senon.modularapp.util.JssSpannableString

/**
 * 商品评价
 * */
@Route(path = GOODS_COMMENT)
class GoodsCommentFragment : BaseFragmentEx() {

    companion object {
        fun newInstance(): GoodsCommentFragment {
            val args = Bundle()
            val fragment = GoodsCommentFragment()
            fragment.arguments = args
            return fragment
        }
    }

    @JvmField
    @Autowired(name = GOODS_ID)
    var goodsId: String?=""

    private lateinit var mTabLayout: TabLayout

    private lateinit var mViewPager: ViewPager

    private val pageLs = listOf(
        SubGoodsCommentFragment.newInstance(
            SubGoodsCommentData(R.string.all_comment, "0")
        ),
        SubGoodsCommentFragment.newInstance(
            SubGoodsCommentData(R.string.comment_pic_video, "1")
        ),
        SubGoodsCommentFragment.newInstance(
            SubGoodsCommentData(R.string.good_comment, "2")
        ),
        SubGoodsCommentFragment.newInstance(
            SubGoodsCommentData(R.string.midi_comment, "3")
        )
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        for (e in pageLs){
            e.setGoodsId(goodsId)
        }
    }

    override fun getLayoutId() = R.layout.goods_comment_fragment

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val mToolbar: Toolbar = view.findViewById(R.id.mToolbar)
        mToolbar.setTitle(R.string.order_comment)
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }

        mTabLayout = view.findViewById(R.id.mTabLayout)
        mViewPager = view.findViewById(R.id.mViewPager)
        mViewPager.adapter = object :
            FragmentStatePagerAdapter(
                childFragmentManager,
                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
            ) {

            override fun getCount() = pageLs.size

            override fun getItem(position: Int) = pageLs[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val sub = getItem(position) as SubGoodsComment
                return AppGlobals.getApplication()
                    .getString(sub.comment.title)
            }

        }

        mTabLayout.setupWithViewPager(mViewPager)
    }

    override fun onBackPressedSupport(): Boolean {
        pop()
        return true

    }
}